package hyl.core.db;

import hyl.core.MyFun;
import hyl.core.conf.MyConst;
import hyl.core.io.MyFile;
import hyl.core.run.MyRun;

/**
 * 数据备份和还原
 * 
 * @author 阿友 37798955@qq.com
 *
 */
public class BackupAndRestore {
	/**
	 * mysqldump 参数<br>
	 * -u: 用户名 -p: 密码<br>
	 * -h指定登陆的主机IP<br>
	 * -P: 端口号，不写默认3306<br>
	 * --all-databases, -A：备份所有数据库<br>
	 * --databases, -B: 用于备份多个数据库，<br>
	 * 没有该选项，把第一个名字参数作为数据库名，后面的作为表名。<br>
	 * 使用该选项，把每个名字都当作为数据库名，还原时无法还原成其他库名。<br>
	 * -d: 只导出数据库的表结构 <br>
	 * -t: 只导出数据库的数据 --quick, -q：快速导出 <br>
	 */
	private static final String MYSQLBACKUP = "cmd /c [DB_PATH]mysqldump [DB_LOGIN] --default-character-set=utf8 --lock-all-tables=true --result-file=[filepath] [dbname]";
	private static final String MYSQLCREATEDB = "cmd /c [DB_PATH]mysqladmin [DB_LOGIN] create [dbname]";
	private static final String MYSQLRESTORE = "cmd /c [DB_PATH]mysql [DB_LOGIN] --default-character-set=utf8 [dbname] < \"[filepath]\"";
	private String dbpath = MyConst.getInstance().get("MYSQL_PATH");
	private String dblogin = MyConst.getInstance().get("MYSQL_LOGIN");
	private String backpath = MyConst.getInstance().get("DB_BACKPATH");

	public static BackupAndRestore configMysql(String dbpath, String dblogin, String backpath) {
		BackupAndRestore bar = new BackupAndRestore();
		System.out.println(dbpath);
		System.out.println(dblogin);
		System.out.println(backpath);
		if (!MyFun.isEmpty(dbpath))
			bar.dbpath = dbpath;
		if (!MyFun.isEmpty(dblogin))
			bar.dblogin = dblogin;
		if (!MyFun.isEmpty(backpath)) {
			bar.backpath = backpath;
		}
		return bar;
	}

	public static BackupAndRestore configMysql() {
		BackupAndRestore bar = new BackupAndRestore();
		return bar;
	}

	/**
	 * 备份数据库
	 * 用例：BackupAndRestore.backupMysql("dls15",MyDate.getYesterday("yyyyMMdd"));
	 * 
	 * @param dbname   备份数据库名称
	 * @param filename 文件名 不含目录和后缀名
	 * @return
	 */
	public boolean backupMysql(String dbname, String filename) {
		if (!MyFile.exist(backpath)) {
			MyFile.openDir(backpath);
		}
		String filepath = backpath + filename + ".sql";
		if (MyFile.exist(filepath)) {
			System.out.println("备份文件已经存在");
			return true;
		}
		String s = MYSQLBACKUP.replace("[DB_PATH]", dbpath);
		s = s.replace("[DB_LOGIN]", dblogin);
		s = s.replace("[filepath]", filepath);
		s = s.replace("[dbname]", dbname);
		System.out.println(s);
		boolean flag = MyRun.exec(s);
		if (flag)
			System.out.println("备份完成");
		else
			System.out.println("备份失败");
		return flag;
	}

	/**
	 * * 还原数据库<br>
	 * 用例：
	 * BackupAndRestore.restoreMysql("dls16",MyDate.getYesterday("yyyyMMdd"));<br>
	 * 
	 * @param db       目标数据库名
	 * @param filename 文件名 不含目录和后缀名
	 */
	public boolean restoreMysql(String dbname, String filename) {
		String filepath = backpath + filename + ".sql";
		String s = MYSQLCREATEDB.replace("[DB_PATH]", dbpath);
		s = s.replace("[DB_LOGIN]", dblogin);
		s = s.replace("[dbname]", dbname);
		System.out.println(s);
		MyRun.exec(s);
		s = MYSQLRESTORE.replace("[DB_PATH]", dbpath);
		s = s.replace("[DB_LOGIN]", dblogin);
		s = s.replace("[filepath]", filepath);
		s = s.replace("[dbname]", dbname);
		System.out.println(s);
		boolean flag = MyRun.exec(s);
		if (flag)
			System.out.println("还原完成");
		else
			System.out.println("还原失败");
		return flag;
	}
}
